package com.future.orm.util;

import com.future.orm.mapping.MappedStatement;
import com.future.orm.mapping.SqlCommandType;
import com.future.orm.session.Configuration;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

/**
 * Mapper文件解析器
 *
 * @author shiyong
 * 2020/5/20 7:58
 */
public class XMLMapperBuilder {

    private Configuration configuration;

    public XMLMapperBuilder(Configuration configuration) {
        this.configuration = configuration;
    }

    /**
     * 解析Mapper文件
     *
     * @param inputStream Mapper文件流
     * @author shiyong
     * 2020/5/20 8:02
    */
    public void parse(InputStream inputStream) throws ClassNotFoundException, DocumentException {
        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();
        String namespace = rootElement.attributeValue("namespace");
        List<Element> selectList = rootElement.elements("select");

        for (Element element : selectList) {
            String id = element.attributeValue("id");
            String statementId = namespace + "." + id;
            String parameterType = element.attributeValue("parameterType");
            String resultType = element.attributeValue("resultType");
            String sql = element.getTextTrim();

            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParameterType(getParameterClassType(parameterType));
            mappedStatement.setResultType(getResultClassType(resultType));
            mappedStatement.setSql(sql);
            mappedStatement.setSqlCommandType(SqlCommandType.SELECT);

            configuration.getMappedStatementMap().put(statementId, mappedStatement);
        }

        List<Element> insertList = rootElement.elements("insert");

        for (Element element : insertList) {
            String id = element.attributeValue("id");
            String statementId = namespace + "." + id;
            String parameterType = element.attributeValue("parameterType");
            String sql = element.getTextTrim();

            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParameterType(getParameterClassType(parameterType));
            mappedStatement.setSql(sql);
            mappedStatement.setSqlCommandType(SqlCommandType.INSERT);

            configuration.getMappedStatementMap().put(statementId, mappedStatement);
        }

        List<Element> updateList = rootElement.elements("update");

        for (Element element : updateList) {
            String id = element.attributeValue("id");
            String statementId = namespace + "." + id;
            String parameterType = element.attributeValue("parameterType");
            String sql = element.getTextTrim();

            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParameterType(getParameterClassType(parameterType));
            mappedStatement.setSql(sql);
            mappedStatement.setSqlCommandType(SqlCommandType.UPDATE);

            configuration.getMappedStatementMap().put(statementId, mappedStatement);
        }

        List<Element> deleteList = rootElement.elements("delete");

        for (Element element : deleteList) {
            String id = element.attributeValue("id");
            String statementId = namespace + "." + id;
            String parameterType = element.attributeValue("parameterType");
            String sql = element.getTextTrim();

            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParameterType(getParameterClassType(parameterType));
            mappedStatement.setSql(sql);
            mappedStatement.setSqlCommandType(SqlCommandType.DELETE);

            configuration.getMappedStatementMap().put(statementId, mappedStatement);
        }
    }

    private Class<?> getParameterClassType(String parameterType) throws ClassNotFoundException {
        if (parameterType == null || parameterType.equals("")) {
            return String.class;
        }

        return Class.forName(parameterType);
    }

    private Class<?> getResultClassType(String resultType) throws ClassNotFoundException {
        if (resultType == null || resultType.equals("")) {
            return null;
        }

        return Class.forName(resultType);
    }
}
